﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity;
using Microsoft.Practices.ObjectBuilder2;
using EntLibContrib.Query.Queries;

namespace EntLibContrib.Query.Configuration.Unity
{
	/// <summary>
	/// This type supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
	/// Represents the process to create the container policies required to create an <see cref="WcfServiceQuery"/>.
	/// </summary>
	public sealed class WcfServiceQueryPolicyCreator : IContainerPolicyCreator
	{
		#region Public Methods
		/// <summary>
		/// Adds the policies required to create the provider specified by <paramref name="configurationObject"/>
		/// to <paramref name="policyList"/>.
		/// </summary>
		/// <param name="policyList">The <see cref="T:Microsoft.Practices.ObjectBuilder2.IPolicyList"/> to which the policies must be added.</param>
		/// <param name="instanceName">The name to use for the instance's policies.</param>
		/// <param name="configurationObject">The <see cref="T:System.Configuration.ConfigurationElement"/> that describes a provider.</param>
		/// <param name="configurationSource">The <see cref="T:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.IConfigurationSource"/> from which additional configuration information
		/// should be retrieved if necessary.</param>
		void IContainerPolicyCreator.CreatePolicies(IPolicyList policyList,
																								string instanceName,
																								ConfigurationElement configurationObject,
																								IConfigurationSource configurationSource)
		{
			WcfServiceQueryData castConfigurationObject = (WcfServiceQueryData)configurationObject;

			new PolicyBuilder<WcfServiceQuery, WcfServiceQueryData>(instanceName,
																															castConfigurationObject,
																															c => new WcfServiceQuery(c.Name,
																																											c.Endpoint,
																																											c.Address,
																																											c.Service,
																																											c.Method,
																																											Resolve.OptionalReference<ICustomCommand>(c.CustomCommandName),
																																											Resolve.OptionalReference<IParameterSet>(c.ParameterSetName),
																																											QueryAssemblerHelper.CreateInstrumentationProvider(c.Name,
																																																																				configurationSource,
																																																																				null))
																															).AddPoliciesToPolicyList(policyList);
		}
		#endregion
	}
}
